from django.shortcuts import render
from  .models import *
from rest_framework.views import APIView
from rest_framework.response import Response
# Create your views here.



#登录
class Deng(APIView):
    def post(self,request):
        name=request.data.get('name')
        password=request.data.get('password')
        try:
            user=Lu.objects.get(name=name)
        except:
            return Response({'code':202,'msg':'用户名不存在'})
        if user.password !=password:
            return Response({'code':204,'msg':'密码错误'})
        return Response({'code':201,'msg':'登录成功'})


#仓库名
class Ming(APIView):
    def get(self,request):
        set=Cang.objects.all()
        list=[]
        for i in set:
            list.append({
                'id':i.id,
                'name':i.name
            })
        return Response(list)



#仓库类型
class Xing(APIView):
    def get(self,request):
        set=request.query_params.get('id')
        se=Ku.objects.filter(ss=set)
        list=[]
        for i in se:
            list.append({
                'id':i.id,
                'name':i.name,
                'hao':i.hao,
                'cheng':i.cheng,
                'lian':i.lian,
                'ss_id':i.ss.id,
                'ss':i.ss.name,
            })
        return Response(list)
    def post(self,request):
        name=request.data.get('name')
        hao=request.data.get('hao')
        cheng=request.data.get('cheng')
        lian=request.data.get('lian')
        ss=request.data.get('ss')
        Ku.objects.create(
            name=name,
            hao=hao,
            cheng=cheng,
            lian=lian,
            ss_id=ss
        )
        return Response({'code':201,'msg':'添加成功'})


#查看
class Xin(APIView):
    def get(self,request,id):
        se=Ku.objects.filter(id=id)
        list=[]
        for i in se:
            list.append({
                'id':i.id,
                'name':i.name,
                'hao':i.hao,
                'cheng':i.cheng,
                'lian':i.lian,
                'ss_id':i.ss.id,
                'ss':i.ss.name,
            })
        return Response(list)